//
//  WDWorkOrderTopView.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/8/16.
//

#import "WDWorkOrderTopView.h"
#import "SearchLeftImageTextField.h"

@interface WDWorkOrderTopView () <UITextFieldDelegate>

/// 筛选
@property (nonatomic, strong) UIButton *btnFiltrate;

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong) UIButton *btnSearch;

@property (nonatomic, strong) UIImageView *searchImgView;

/// 垂直线
@property (nonatomic, strong) CALayer *verticalLine;

@property (nonatomic, strong) UITextField *searchTextField;

@property (nonatomic, strong) UILabel *searchLabel;

@end

@implementation WDWorkOrderTopView

- (void)initWithSubView {
    self.backgroundColor = [UIColor whiteColor];
    
    /// 筛选
    [self initWithBtnFiltrate];
    
    [self initWithBgView];
    
    [self initWithBtnSearch];
    
    [self initWithSearchImgView];
    
    [self initWithVerticalLine];
    
//    [self initWithSearchTextField];
    
    [self initWithSearchLabel];
}

/// 筛选
- (void)initWithBtnFiltrate {
    if (!_btnFiltrate) {
        UIImage *image = [UIImage imageNamed:@"ic_order_filtrate"];
        CGRect frame = CGRectMake(self.width - image.size.width - 20, (self.height - image.size.height) * 0.5, image.size.width, image.size.height);
        
        _btnFiltrate = [UIButton createWithButton:self frame:frame font:FONTSIZE(16) target:self action:@selector(actionWithFiltrate:) propertiesBlock:^(UIButton * _Nonnull button) {
            [button setImage:image forState:UIControlStateNormal];
        }];
    }
}

- (void)initWithBgView {
    if (!_bgView) {
        CGRect frame = CGRectMake(20, (self.height - 36) * 0.5, self.btnFiltrate.left - 20 - 8, 36);
        _bgView = [[UIView alloc] initWithFrame:frame];
        _bgView.layer.cornerRadius = 6;
        _bgView.layer.masksToBounds = YES;
        _bgView.layer.borderColor = kThemeColor.CGColor;
        _bgView.layer.borderWidth = 1;
        [self addSubview:_bgView];
    }
}


- (void)initWithBtnSearch {
    if (!_btnSearch) {
        CGRect frame = CGRectMake(self.bgView.width - 42 - 6, (self.bgView.height - 26) * 0.5, 42, 26);
        _btnSearch = [UIButton createWithButton:self.bgView frame:frame font:[UIFont systemFontOfSize:12 weight:UIFontWeightRegular] target:self action:@selector(actionWithSearch:) propertiesBlock:^(UIButton * _Nonnull button) {
            [button setTitle:@"搜索" forState:UIControlStateNormal];
            [button setTitleColor:kThemeColor forState:UIControlStateNormal];
            button.backgroundColor = [UIColor colorWithHexString:@"#d1f3e2"];
            button.layer.cornerRadius = 6;
            button.layer.masksToBounds = YES;
        }];
    }
}

- (void)initWithSearchImgView {
    if (!_searchImgView) {
        UIImage *image = [UIImage imageNamed:@"ic_order_search"];
        CGRect frame = CGRectMake(8, (self.bgView.height - image.size.height) * 0.5, image.size.width, image.size.height);
        _searchImgView = [UIImageView createWithImageView:self.bgView frame:frame defaultImage:image propertiesBlock:^(UIImageView * _Nonnull imageView) {
            
        }];
    }
}

- (void)initWithVerticalLine {
    if (!_verticalLine) {
        CGRect frame = CGRectMake(self.searchImgView.right + 10, (self.bgView.height - 18) * 0.5, 1, 18);
        _verticalLine = [CALayer drawLine:self.bgView frame:frame lineColor:[UIColor colorWithHexString:@"#B4B4B7"]];
    }
}

- (void)initWithSearchTextField {
    CGRect frame = CGRectMake(_verticalLine.right + 10, (self.bgView.height - 32) * 0.5, self.btnSearch.left - self.verticalLine.right - 10 - 10, 32);
    _searchTextField = [[UITextField alloc] initWithFrame:frame];
    _searchTextField.placeholder = @"请输入关键词搜索";
    _searchTextField.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
//    _searchTextField.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    _searchTextField.delegate = self;
    _searchTextField.borderStyle = UITextBorderStyleNone;
    _searchTextField.returnKeyType = UIReturnKeySearch;
    _searchTextField.keyboardType = UIKeyboardTypeDefault;
    _searchTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _searchTextField.enabled = NO;
    [_bgView addSubview:_searchTextField];
    
    @weakify(self);
    UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        @strongify(self);
        debugLog(@"eee");
    }];
    [self.searchTextField addGestureRecognizer:tap];
    self.searchTextField.userInteractionEnabled = YES;
}

- (void)initWithSearchLabel {
    if (!_searchLabel) {
        CGRect frame = CGRectMake(_verticalLine.right + 10, (self.bgView.height - 32) * 0.5, self.btnSearch.left - self.verticalLine.right - 10 - 10, 32);
        _searchLabel = [UILabel createWithLabel:self.bgView frame:frame font:[UIFont systemFontOfSize:12 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#B4B4B7"];
            label.text = @"请输入关键词搜索";
        }];
        
        @weakify(self);
        UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
            @strongify(self);
            [self actionWithSearch:nil];
        }];
        [self.searchLabel addGestureRecognizer:tap];
        self.searchLabel.userInteractionEnabled = YES;
    }
}


/// 筛选
/// - Parameter sender: sender description
- (void)actionWithFiltrate:(id)sender {
    if (self.touchFiltrateBlock) {
        self.touchFiltrateBlock();
    }
}

/// 搜索
/// - Parameter sender: sender description
- (void)actionWithSearch:(id)sender {
    [self.searchTextField resignFirstResponder];
    if (self.touchSearchBlock) {
        self.touchSearchBlock(self.searchTextField.text);
    }
}

- (void)setSearchMsg:(NSString *)searchMsg {
    _searchTextField.text = searchMsg;
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self actionWithSearch:nil];
    return NO;
}

- (void)textFieldDidChange:(UITextField *)textField
{
    
}


@end
